import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.params.SetParams;

import java.util.Set;

/**
 * @Author baekhyun
 **/
public class RedisDemo {
    public static void test1(Jedis jedis){
        System.out.println("get和set的使用");
        //先清空数据库
        jedis.flushAll();

        //set
        jedis.set("key","111");
        jedis.set("key2","222");
        SetParams params=new SetParams();
        params.ex(10);
        params.xx();
        jedis.set("key","333",params);

        //get
        String value=jedis.get("key");
        System.out.println("value="+value);
    }

    public static void test2(Jedis jedis){
        System.out.println("exists和del");
        jedis.flushAll();

        jedis.set("key","111");
        jedis.set("key2","222");

        boolean result=jedis.exists("key");
        System.out.println("result:"+result);

        long result2=jedis.del("key");
        System.out.println("result2:"+result2);

        result=jedis.exists("key");
        System.out.println("result:"+result);
    }

    public static void test3(Jedis jedis){
        System.out.println("keys");
        jedis.flushAll();

        jedis.set("key","111");
        jedis.set("key2","222");
        jedis.set("key3","333");
        jedis.set("key4","444");

        Set<String> keys=jedis.keys("*");
        System.out.println(keys);
    }

    public static void test4(Jedis jedis){
        System.out.println("expire和ttl");
        jedis.flushAll();

        jedis.set("key","111");
        jedis.expire("key",10);

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Long time=jedis.ttl("key");
        System.out.println("time:"+time);
    }

    public static void test5(Jedis jedis){
        System.out.println("type");
        jedis.flushAll();

        jedis.set("key","111");
        String type=jedis.type("key");
        System.out.println("type:"+type);

        jedis.lpush("key2","111","222","333");
        type=jedis.type("key2");
        System.out.println("type:"+type);

        jedis.hset("key3","f1","111");
        type=jedis.type("key3");
        System.out.println("type:"+type);

        jedis.sadd("key4","111","222","333");
        type=jedis.type("key4");
        System.out.println("type:"+type);

        jedis.zadd("key5",10,"zhangsan");
        type=jedis.type("key5");
        System.out.println("type:"+type);
    }

    public static void main(String[] args) {
        //连接到Redis服务器上
        JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
        try (Jedis jedis=jedisPool.getResource()){
            jedis.auth("19930112@do&baekhyun");
            //redis的各种命令，就都对应到jedis对象的各种方法
//            String pong=jedis.ping();
//            System.out.println(pong);

            //test1(jedis);
            //test2(jedis);
            //test3(jedis);
            //test4(jedis);
            test5(jedis);
        }
    }
}
